home *** CD-ROM | disk | FTP | other *** search
- Path: hubcap.clemson.edu!hubcap!mjs
- From: mjs@hubcap.clemson.edu (M. J. Saltzman)
- Newsgroups: comp.programming,comp.lang.c
- Subject: Re: A weird thing about printf()
- Followup-To: comp.lang.c
- Date: 10 Apr 96 14:49:46 GMT
- Organization: Clemson University
- Message-ID: <mjs.829147786@hubcap>
- References: <4kflru$5if@dewey.csun.edu>
- NNTP-Posting-Host: hubcap.clemson.edu
- X-Newsreader: NN version 6.5.0 #1
-
- This question properly belongs in comp.lang.c, to which I have directed
- followups.
-
- kc44097@csun.edu (chen) writes:
-
- > The program :
-
- >#include <stdio.h>
-
- >int answer;
- >main()
- >{
- > answer=2+2;
- > printf("The answer is %d\n");
- > return 0;
- >}
-
- > In printf(),I lost "answer" in the end,but it works,and give me the
- >result 0.I wonder how the compiler handle this condition.Also,is this
-
- It doesn't "work", since it doesn't give the answer you are looking
- for. What probably happens is that printf() looks where the second
- argument *would be* and prints whatever random garbage happens to be
- there, but what's allowed to happen is anything at all, i.e., the
- behavior is undefined.
-
- >allow by C?(by this, I mean is this a leagal usage in language itself,
- >or just a mistake cause by the compiler?)
-
- It's a mistake caused by the programmer! Since printf() is declared
- to take a variable number of arguments, the compiler has no obligation
- to check the number of arguments. Some compilers know that printf()
- is special and check the argument count against the format string, but
- that feature is not all that common (and can only be applied if the
- format string is a constant anyway).
-
- > Can someone give me some advice ? Please e-mail me :
- > kc44097@huey.csun.edu
-
- > Thankx !!!!!!
- --
- Matthew Saltzman
- Clemson University Math Sciences
- mjs@clemson.edu
-